package com.fei.item.service;

import com.fei.item.mapper.SpecGroupMapper;
import com.fei.item.mapper.SpecParamMapper;
import com.fei.item.pojo.SpecGroup;
import com.fei.item.pojo.SpecParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SpecificationService {

    @Resource
    private SpecGroupMapper groupMapper;
    @Resource
    private SpecParamMapper paramMapper;

    public List<SpecGroup> querySpecsByCid(Long cid) {
        // 查询规格组
        List<SpecGroup> groups = this.queryGroupByCid(cid);
        groups.forEach(g -> {
            // 查询组内参数
            g.setParams(this.queryParams(g.getId(), null, null, null));
        });
        return groups;
    }

    /**
     * 根据分类cid查询规格组
     *
     * @param cid
     * @return
     */
    public List<SpecGroup> queryGroupByCid(Long cid) {
        //组装数据
        SpecGroup group = new SpecGroup();
        group.setCid(cid);
        System.out.println("组装的SpecGroup数据："+group.toString());
        //根据cid查询数据库
        List<SpecGroup> select = groupMapper.select(group);
        //遍历规格数据
        for (SpecGroup specGroup : select) {
            System.out.println(specGroup);
        }
        return select;
    }

    /**
     * 根据分类id查询分组
     *
     * @param cid
     * @return
     */
    public List<SpecGroup> queryGroupsByCid(Long cid) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        return this.groupMapper.select(specGroup);
    }

    public int updateGroupsByCid(SpecGroup specGroup) {

        return this.groupMapper.updateByPrimaryKeySelective(specGroup);
    }

    public int deleteGroupsByCid(Long id) {

        return this.groupMapper.deleteByPrimaryKey(id);
    }

    public int addGroupsByCid(SpecGroup specGroup) {

        return this.groupMapper.insertSelective(specGroup);
    }


    /**
     * 根据条件查询规格参数
     * @param gid
     * @return
     */
//    public List<SpecParam> queryParams(Long gid) {
//        SpecParam param = new SpecParam();
//        param.setGroupId(gid);
//        return this.paramMapper.select(param);
//    }
    /**
     * 根据gid查询规格参数
     * @param gid
     * @return
     */
    public List<SpecParam> queryParams(Long gid, Long cid, Boolean generic, Boolean searching) {
        SpecParam record = new SpecParam();
        record.setGroupId(gid);
        record.setCid(cid);
        record.setGeneric(generic);
        record.setSearching(searching);
        return this.paramMapper.select(record);
    }
}
